package cn.hp.controller;

import cn.hp.service.MasterService;
import cn.hp.utils.JsonData;
import com.alibaba.druid.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.json.Json;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/waiter")
public class WaiterController {

    @Autowired
    private MasterService masterService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonData login(String account, String password, String verifyCode, HttpSession session){
        String code= (String) session.getAttribute("code");
        if(StringUtils.isEmpty(account)||StringUtils.isEmpty(password)){
            return JsonData.buildError("账号密码不能为空");
        }
        if(!StringUtils.equalsIgnoreCase(code,verifyCode)){
            return JsonData.buildError("验证码错误");
        }

        JsonData jsonData =masterService.login(account, password);
        session.setAttribute("user",jsonData.getData());
        return jsonData;
    }
}
